Flutter 拓展之GlobalKey

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//  @Description: GlobalKey拓展
// @Author: 陈胜辉
// @Date: 2019-12-26 15:37:34
// @Version: 版本号, YYYY-MM-DD
// @LastEditors: 陈胜辉
// @LastEditTime: 2019-12-26 16:07:28
// @Deprecated: 否
// 备注
import 'package:flutter/material.dart';

extension SHExtension on GlobalKey {
static Offset offset = Offset.zero;

void setOffset(Offset off) {
offset = off;
}

RenderBox _render() {
if (this.currentContext != null) {
return this.currentContext.findRenderObject();
}
return null;
}

double x() {
return this.origin().dx;
}

double y() {
return this.origin().dy;
}

double width() {
return this.size().width;
}

double height() {
return this.size().height;
}

Offset origin() {
return this._render().localToGlobal(offset);
}

Size size() {
return this._render().size;
}

double maxX() {
return this.x() + this.width();
}

double maxY() {
return this.y() + this.height();
}

double centerX() {
return this.x() + (this.width() / 2);
}

double centerY() {
return this.y() + (this.height() / 2);
}
}
-------------本文结束感谢阅读-------------